﻿using System;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;

namespace RentIt06
{
    /// <summary>
    /// Contains information about the an account.
    /// Can be used to update the account with UpdateAccount(...) in UserSession.
    /// </summary>
    [DataContract]
    public struct Account
    {
        internal Account(db.User u) {
            if (u != null) {
                Email = u.email;
                Id = u.id;
                Permission = (PermissionGroup) Enum.Parse(typeof (PermissionGroup), u.Role1.name);
                Credit = u.credit;
                FirstName = u.firstName;
                LastName = u.lastname;
                Street = u.address;
                ZipCode = u.zipCode;
                City = u.city;
                Country = u.country;
                PhoneNumber = u.phoneNumber;
            } else {
                Email = default(string);
                Id = default(int);
                Credit = default(decimal);
                Permission = default(PermissionGroup);
                FirstName = default(string);
                LastName = default(string);
                Street = default(string);
                ZipCode = default(string);
                City = default(string);
                Country = default(string);
                PhoneNumber = default(string);
            }
        }

        internal static byte[] hash(string password, string email) {
            var enc = new ASCIIEncoding();

            string[] split = email.Split('@');
            if (split.Length != 2)
                split = new string[] {email, email};
            byte[] input = enc.GetBytes(split[1] + password + split[0] + "OmGsAlT!John");
            SHA512 sha = SHA512.Create();
            return sha.ComputeHash(input);
        }

        /// <summary>
        /// The email of the account. Must be unique between all accounts.
        /// </summary>
        [DataMember] public string Email;

        /// <summary>
        /// Id of the account. Is auto generated on account creation.
        /// </summary>
        [DataMember] public readonly int Id;

        /// <summary>
        /// The amount of credits on the account. Can only be changed by buying products or adding credits with AddCredits(...) in UserSession.
        /// </summary>
        [DataMember] public readonly decimal Credit;
        
        /// <summary>
        /// Tells if an account is Admin, Publisher or a User.
        /// </summary>
        [DataMember] public readonly PermissionGroup Permission;

        /// <summary>
        /// First name of the account holder.
        /// </summary>
        [DataMember] public string FirstName;

        /// <summary>
        /// Last name of the account holder.
        /// </summary>
        [DataMember] public string LastName;

        /// <summary>
        /// The street account holders address.
        /// </summary>
        [DataMember] public string Street;

        /// <summary>
        /// The zip code of the acount holders address.
        /// </summary>
        [DataMember] public string ZipCode;

        /// <summary>
        /// The city of the account holders address.
        /// </summary>
        [DataMember] public string City;

        /// <summary>
        /// The country of the account holders address.
        /// </summary>
        [DataMember] public string Country;

        /// <summary>
        /// The phonenumber of the account holder.
        /// </summary>
        [DataMember] public string PhoneNumber;
    }
}